package com.hyb.controller;


import com.alibaba.fastjson.JSON;
import com.hyb.Vo.SaleStaffVo;
import com.hyb.entity.Sale;
import com.hyb.page.PageReq;
import com.hyb.response.Result;
import com.hyb.service.SaleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  销售计划Controller
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/sale")
public class SaleController {

    @Autowired
    private SaleService saleService;

    @RequestMapping(value = "/createSale",method = RequestMethod.POST)
    @ApiOperation("新建销售机会")
    public String createSale(@RequestBody Sale sale){
        saleService.createSale(sale);
        return JSON.toJSONString(new Result().setCode(200).setMessage("保存成功"));
    }

    @RequestMapping(value = "/loadAllSale",method = RequestMethod.GET)
    @ApiOperation("获取所有的销售机会")
    public String loadAllSale(Integer limit, Integer current,Integer staffId,String saleState){
        PageReq allSales = saleService.getAllSales(limit, current, staffId, saleState);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(allSales));
    }

    @RequestMapping(value = "/loadSale",method = RequestMethod.GET)
    @ApiOperation("根据id获取销售机会信息")
    public String loadSale(Integer saleId){
        SaleStaffVo saleBySaleId = saleService.getSaleBySaleId(saleId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(saleBySaleId));
    }

    @RequestMapping(value = "/assignAccountManager",method = RequestMethod.PUT)
    @ApiOperation("指派开发者")
    public String assignAccountManager(@RequestBody Sale sale){
        saleService.assignAccountManager(sale);
        return JSON.toJSONString(new Result().setCode(200).setMessage("指派成功"));
    }

    @RequestMapping(value = "/getSalesByAccountManagers",method = RequestMethod.GET)
    @ApiOperation("获取客户经理的销售机会")
    public String getSalesByAccountManagers(Integer limit, Integer current, Integer staffId, String saleState){
        PageReq salesByAccountManagers = saleService.getSalesByAccountManagers(limit, current, staffId, saleState);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(salesByAccountManagers));
    }

    @RequestMapping(value = "/updateSale",method = RequestMethod.PUT)
    @ApiOperation("修改销售机会")
    public String updateSale(@RequestBody Sale sale){
        saleService.updateSale(sale);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改成功"));
    }

    @RequestMapping(value = "/deleteSale",method = RequestMethod.DELETE)
    @ApiOperation("删除销售机会")
    public String deleteSale(Integer saleId){
        saleService.deleteSale(saleId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("删除成功"));
    }

    @RequestMapping(value = "/loadAllAssignedSales",method = RequestMethod.GET)
    @ApiOperation("获取所有已指派的销售机会")
    public String loadAllAssignedSales(Integer limit,Integer current){
        PageReq allAssignedSales = saleService.getAllAssignedSales(limit, current);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(allAssignedSales));
    }

    @RequestMapping(value = "/loadAllAssignedSalesByAccountManager",method = RequestMethod.GET)
    @ApiOperation("根据员工id获取已指派的销售机会")
    public String loadAllAssignedSalesByAccountManager(Integer limit,Integer current,Integer staffId){
        PageReq allAssignedSalesByAccountManager = saleService.getAllAssignedSalesByAccountManager(limit, current, staffId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(allAssignedSalesByAccountManager));
    }

    @RequestMapping(value = "/completeDevelopment",method = RequestMethod.PUT)
    @ApiOperation("完成开发")
    public String completeDevelopment(@RequestBody Sale sale){
        saleService.completeDevelopment(sale.getSaleId());
        return JSON.toJSONString(new Result().setCode(200).setMessage("完成开发"));
    }

    @RequestMapping(value = "/terminationOfDevelopment",method = RequestMethod.PUT)
    @ApiOperation("终止开发")
    public String terminationOfDevelopment(@RequestBody Sale sale){
        saleService.terminationOfDevelopment(sale.getSaleId());
        return JSON.toJSONString(new Result().setCode(200).setMessage("终止成功"));
    }
}
